from flask_restful import Resource
from flask_restful.reqparse import RequestParser
from app.serve.content.column import ColumnServe
from app.serve.user.auth import login_required
from app.util.exception import WtreesReturn, ErrorType


class ColumnCreate(Resource):
    """
     新建和修改栏目名称
    """
    post_data = RequestParser()
    post_data.add_argument("id", type=str, help='栏目ID')
    post_data.add_argument("column_name", type=str, help='栏目名称')

    """
       搜索栏目和栏目列表
    """
    get_data = RequestParser()
    get_data.add_argument("column_name", type=str, help='栏目名称')

    @login_required
    def post(self):
        args = self.post_data.parse_args()
        ColumnServe().verify(args['column_name'])
        ColumnServe().column_create(args['column_name'])
        return WtreesReturn.data(ErrorType.OK)

    @login_required
    def get(self):
        args = self.post_data.parse_args()
        data = ColumnServe().list(column_name=args['column_name'])
        return WtreesReturn.data(ErrorType.OK, data=data.items, page=data.page_info())

